package com.application.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import com.application.bean.Items;
import com.application.bean.Menu;
import com.application.bean.MenuConfig;
import com.application.bean.MenuConfigProperty;
import com.application.bean.ModelMode;
import com.application.bean.ModelModeChild;
import com.application.pojo.Auth;
import com.application.pojo.MenuMessage;
import com.application.service.Bsm;
@Configuration
@Order(0)
public class LoadONStandUp implements CommandLineRunner {
	@Autowired
	Bsm b;
	@Autowired
	MenuConfigProperty menuConfigProperty;
	@Override
	public void run(String... arg0) throws Exception {
		// TODO Auto-generated method stub
		System.err.println("----------- start exec load modul ---------");
		for (MenuConfig menuConfig : menuConfigProperty.getMenuConfigs()) {
			
			FirstMenu(menuConfig);
			
			for (Menu menu : menuConfig.getMenu()) {
				
				SecondMenu(menu,menuConfig.getId());
				
				for (Items items : menu.getItems()) {
					ThirdMenu(items,menu.getId());
				}
			}
		}
	}
	
	// 一级菜单
	public void FirstMenu(MenuConfig menuConfig){
		MenuMessage mm = new MenuMessage();
		String menuId = menuConfig.getId();
		mm.setMenuId(menuId);
		mm.setCollapsed(menuConfig.isCollapsed());
		mm.setHomePage(menuConfig.getHomePage());
		if(b.exitMenu(menuId)){
			b.saveMenu(mm);
		}
	}
	
	
	//二级菜单
	public void SecondMenu(Menu menu,String parentId){
		MenuMessage mm = new MenuMessage();
		String menuId = menu.getId();
		mm.setMenuId(menuId);
		mm.setText(menu.getText());
		mm.setParentId(parentId);
		if(b.exitMenu(menuId)){
			b.saveMenu(mm);
		}
	}
	
	
	public void ThirdMenu(Items items,String patentId){
		
		MenuMessage mm = new MenuMessage();
		String menuId = items.getId();
		mm.setMenuId(menuId);
		mm.setText(items.getText());
		mm.setHref(items.getHref());
		mm.setCloseable(items.isCloseable());
		mm.setParentId(patentId);
		if(b.exitMenu(menuId)){
			b.saveMenu(mm);
		}
		
	}
	
}
